package com.ybliang.plat.common.web;

import com.ybliang.plat.common.data.vo.resp.Resp;
import com.ybliang.plat.serviceapp.common.exception.BusinessException;
import com.ybliang.plat.serviceapp.common.exception.Errors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    public Resp BadRequestExceptionHandler(Exception e) {
        String error = "";
        if(e instanceof HttpRequestMethodNotSupportedException) {
            LOGGER.error("GlobalExceptionHandler.HttpRequestMethodNotSupportedException error...", e);
            HttpRequestMethodNotSupportedException ex = (HttpRequestMethodNotSupportedException) e;
            error = ex.getMessage();
        }
        return Resp.fail(Errors.CheckError.BAD_REQUEST_400, error);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    public Resp MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        LOGGER.error("GlobalExceptionHandler.MethodArgumentNotValidExceptionHandler error...", e);
        String error = e.getBindingResult().getFieldError().getDefaultMessage();
        return Resp.fail(Errors.CheckError.BAD_REQUEST_400, error);
    }

    @ExceptionHandler(HttpMessageConversionException.class)
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    public Resp HttpMessageConversionExceptionHandler(HttpMessageConversionException e){
        LOGGER.error("GlobalExceptionHandler.HttpMessageConversionExceptionHandler error...", e);
        String error = e.getCause().getMessage();
        return Resp.fail(Errors.CheckError.BAD_REQUEST_400, error);

    }

    @ExceptionHandler(BusinessException.class)
    @ResponseStatus
    public Resp BusinessExceptionHandler(BusinessException e) {
        LOGGER.error("GlobalExceptionHandler.BusinessExceptionHandler error...", e);
        return Resp.fail(e.code, e.message);
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Resp NoHandlerFoundExceptionHandler(NoHandlerFoundException e){
        LOGGER.error("GlobalExceptionHandler.NoHandlerFoundExceptionHandler error...", e);
        String error = e.getMessage();
        return Resp.fail(Errors.CheckError.BAD_REQUEST_404, error);
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Resp ExceptionHandler(Exception e) {
        LOGGER.error("GlobalExceptionHandler.ExceptionHandler error...", e);
        return Resp.fail(Errors.SystemError.SYS_ERROR, e.getMessage());
    }

}
